package project;
import project.fileAccess.*;
//import java.io.*;
import java.util.ArrayList;

public class Sync {
   // public static ArrayList<RecordInfo> changedRecords = new ArrayList<RecordInfo>();
public static void main(String[] args){
    ReadLocations rdloc = new ReadLocations();
    ArrayList<DatabaseInfo> dbList = rdloc.getDbList();
    Checksum chksmLocal = new Checksum();
 //   System.out.println(chksmLocal.getCheckSum());
    for (int j=0;j<dbList.size();j++){
//        System.out.println(dbList.get(j).getDatabaseName());
        Checksum chksm = null;
        try{
        chksm = new Checksum(dbList.get(j),1,1);
        } catch (SingleRecordException e){
        }
       // System.out.println(chksmLocal.getCheckSum() + " local, " + chksm.getCheckSum() + " other");
   //     System.out.println("Checksum " + chksm.getCheckSum());
        if (chksm.getCheckSum() != chksmLocal.getCheckSum()){
            //
//            int rows = chksm.getRowNumbers(dbList.get(j));
//            int a = rows/2;
            try{
            reCount(dbList.get(j),2,1, chksmLocal.getCheckSum(), chksm.getCheckSum());
            reCount(dbList.get(j),2, 2, chksmLocal.getCheckSum(), chksm.getCheckSum());
            } catch (SingleRecordException e){

            }
        } else {
            System.out.println("Databases match");
        }
    }
    RecordsChanged r = new RecordsChanged();
    r.resolveClashes();
    r.performUpdates();
}

public static void reCount(DatabaseInfo database, int fraction, int position, int prevLocal, int prevOther)throws SingleRecordException{
    System.out.print("Fraction " +fraction);
        System.out.println(", Position " +position);
    Checksum chkLocal = null;
    //try{
    chkLocal = new Checksum(fraction, position);
//    } catch (SingleRecordException e){
// System.out.println("£Local: "+chkLocal.getCheckSum());
//
//    }
    Checksum chkOther = null;
    try{
    chkOther = new Checksum(database, fraction, position);
   // System.out.println("     Local: "+chkLocal.getCheckSum()+", Other: "+ chkOther.getCheckSum());
    } catch (SingleRecordException e){
       }
    try{
    if (chkLocal.getCheckSum() != chkOther.getCheckSum()){// && (finish - start) > 0){
        try{
        reCount(database, fraction *2, (position *2) -1, chkLocal.getCheckSum(), chkOther.getCheckSum());
        } catch (SingleRecordException e){
           RecordInfo rci = new RecordInfo(database, position *2 -1);
                    RecordsChanged r = new RecordsChanged();
    r.addRecord(rci);
        }
    }
        if (chkLocal.getCheckSum() != chkOther.getCheckSum()){
            try{
        reCount(database, fraction *2, position *2,  chkLocal.getCheckSum(), chkOther.getCheckSum());
            } catch (SingleRecordException e){
                RecordInfo rci = new RecordInfo(database, position* 2);
                    RecordsChanged r = new RecordsChanged();
    r.addRecord(rci);
            }
    }
} catch (NullPointerException e){

}
}

}
